<template>
  <el-card>
    <template #header>
      <h2>护工【{{ nurseName }}】的服务客户护理记录</h2>
    </template>

    <!-- 搜索条件 -->
    <el-form :inline="true" :model="filters" class="toolbar">
      <el-form-item label="客户名">
        <el-input
          v-model="filters.name"
          placeholder="输入客户名，可空"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 客户列表 -->
    <el-table
      :data="customers"
      stripe
      style="margin-top:16px;"
    >
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="name" label="客户姓名" width="100" />
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column label="性别" width="80">
        <template #default="{ row }">
          <span>{{ row.gender === 0 ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="roomNumber" label="房间号" width="60" />
      <el-table-column prop="bedNumber" label="床号" />
      <el-table-column prop="careLevelName" label="护理级别" width="120" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="small"
            type="primary"
            @click="viewRecords(row.id, row.name)"
          >查看护理记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      style="margin-top:16px; text-align:right;"
      background
      layout="prev, pager, next, jumper, ->, total"
      :current-page="page.current"
      :page-size="page.size"
      :total="page.total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    />
  </el-card>
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import { useRouter } from 'vue-router'
import qs from 'qs'
import { jwtDecode } from 'jwt-decode'

// 注入 axios 实例
const axios = inject('axios')
const router = useRouter()

// 搜索条件
const filters = reactive({ name: '' })
// 客户列表数据
const customers = ref([])

// 分页相关状态
const page = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 获取当前登录护工信息
const nurseId = ref('')
const nurseName = ref('')
function initUser() {
  const token = sessionStorage.getItem('token')
  if (token) {
    const payload = jwtDecode(token)
    const user = JSON.parse(payload.mobileJson)
    nurseId.value = user.id
    nurseName.value = user.realName
  }
}
initUser()

// 查询客户列表
async function fetchCustomers() {
  try {
    const res = await axios.post(
      'CheckOutRegistrationController/searchClient',
      qs.stringify({
        healthManagerId: nurseId.value,
        clientName: filters.name,
        cur: page.current,
        number: page.size
      })
    )
    const rb = res.data
    if (rb.status === 200) {
      customers.value = rb.data
      page.total = rb.total
    } else {
      ElMessage.error('加载失败：' + rb.message)
    }
  } catch (err) {
    ElMessage.error('网络错误')
  }
}

// 点击查询按钮
function onSearch() {
  page.current = 1
  fetchCustomers()
}

// 分页页码变化
function handlePageChange(newPage) {
  page.current = newPage
  fetchCustomers()
}

// 每页条数变化
function handleSizeChange(newSize) {
  page.size = newSize
  page.current = 1
  fetchCustomers()
}

// 跳转查看护理记录详情
function viewRecords(customerId, customerName) {
  router.push({
    path: '/RecordDetail2',
    query: { customerId, name: customerName }
  })
}

// 组件挂载后自动加载
onMounted(fetchCustomers)
</script>

<style scoped>
.toolbar {
  margin-bottom: 16px;
}
</style>
